package com.srlg.service;

import com.srlg.model.entity.User;
import io.quarkus.hibernate.orm.PersistenceUnit;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityGraph;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.query.Query;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: srlg li
 * @date: 2024/12/26 下午11:11
 */
@ApplicationScoped
public class UserService {

//    @Inject
//    Session session;

    @Inject
    EntityManager entityManager;

    @Transactional
    public String save(String name, String email) {
        User user = new User();
        user.setName(name);
        user.setEmail(email);
//        user.persist();

        entityManager.persist(user);

        return user.toString();
    }

    public String namesService() {
//        List<User> greetings = User.listAll();
        List<User> rl = entityManager.createNamedQuery("Users.findAll", User.class).getResultList();

        return rl.stream().map(User::getName)
            .collect(Collectors.joining(", "));
    }
}
